<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设置 - AI公众号助手</title>
    <link rel="stylesheet" href="/styles.css">
</head>
<body>
    <div class="container">
        <div class="settings-container">
            <div class="settings-box">
                <a href="/" class="back-home">← 返回首页</a>
                <h2>公众号设置</h2>
                <p class="settings-subtitle">配置您的微信公众号信息</p>
                
                <form id="settingsForm" class="settings-form">
                    <div class="form-group">
                        <label for="appid">AppID</label>
                        <input type="text" id="appid" name="appid" placeholder="请输入微信公众号的AppID" required>
                        <p class="form-tip">在微信公众平台后台的"开发 > 基本配置"中可以找到</p>
                    </div>
                    
                    <div class="form-group">
                        <label for="secret">AppSecret</label>
                        <input type="text" id="secret" name="secret" placeholder="请输入微信公众号的AppSecret" required>
                        <p class="form-tip">在微信公众平台后台的"开发 > 基本配置"中可以找到</p>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="save-btn">保存设置</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        // 页面加载时获取当前设置
        window.addEventListener('load', function() {
            loadSettings();
        });

        // 加载设置
        function loadSettings() {
            fetch('/user/settings')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        document.getElementById('appid').value = data.appid || '';
                        document.getElementById('secret').value = data.secret || '';
                    } else {
                        if (data.message === '请先登录') {
                            window.location.href = '/login';
                        } else {
                            alert(data.message);
                        }
                    }
                })
                .catch(error => {
                    console.error('获取设置失败:', error);
                    alert('获取设置失败，请稍后重试');
                });
        }

        // 保存设置
        document.getElementById('settingsForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const formData = {
                appid: document.getElementById('appid').value,
                secret: document.getElementById('secret').value
            };
            
            fetch('/user/settings', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('设置保存成功！');
                } else {
                    alert(data.message || '设置保存失败，请稍后重试');
                }
            })
            .catch(error => {
                console.error('保存设置失败:', error);
                alert('保存设置失败，请稍后重试');
            });
        });
    </script>
</body>
</html> 